﻿using Bot.Application.Dto;
using Bot.Domain;
using Bot.Domain.Entities;
using Bot.Domain.Enum;
using Bot.Domain.Interfaces;
using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Bot.Application
{   
    public interface IQuickReplyVerifyDataManager
    {
        ErrorInfo VerifyId(Guid id);
        ErrorInfo VerifyType(string type);
        ErrorInfo VerifyCreate(Guid botId, QuickReplyCreateDto dto);
        ErrorInfo VerifyUpdate(QuickReplyUpdateDto dto);        
    }
    public class QuickReplyVerifyDataManager : IQuickReplyVerifyDataManager
    {
        private readonly IQuickReplyDomainService _quickReplyDomainService;
        private readonly IQuickReplyItemVerifyDataManager _quickReplyItemVerifyService;
        public QuickReplyVerifyDataManager(IQuickReplyDomainService quickReplyDomainService,
            IQuickReplyItemVerifyDataManager quickReplyItemVerifyService)
        {
            _quickReplyDomainService = quickReplyDomainService;
            _quickReplyItemVerifyService = quickReplyItemVerifyService;
        }
        public ErrorInfo VerifyId(Guid id)
        {
            ErrorInfo errorInfo = new ErrorInfo { Code = 1 };

            var quickReply = _quickReplyDomainService.Get(id);
            if (quickReply == null || quickReply.IsDeleted)
            {
                errorInfo = APIHelper.GetErrorMessageByCode(54205, new object[] { id });
            }         
            return errorInfo;
        }
        private ErrorInfo VerifyIdAndName(Guid id, string name)
        {
            ErrorInfo errorInfo = new ErrorInfo { Code = 1 };

            var quickReply = _quickReplyDomainService.Get(id);
            if (quickReply == null || quickReply.IsDeleted)
            {
                errorInfo = APIHelper.GetErrorMessageByCode(54205, new object[] { id });
            }
            if (errorInfo.Code != 1) return errorInfo;            
            errorInfo = VerifyName(quickReply.BotId, name, id);
            return errorInfo;
        }  
        //name 不能重复
        private ErrorInfo VerifyName(Guid botId, string name, Guid id)
        {
            ErrorInfo errorInfo = new ErrorInfo { Code = 1 };
            var list = _quickReplyDomainService.GetByName(botId, name);
            if (id == Guid.Empty)
            {
                if (list == null || list.Id == Guid.Empty) errorInfo = APIHelper.GetErrorMessageByCode(54201);
            }
            else
            {
                if (list == null || list.Id != id) errorInfo = APIHelper.GetErrorMessageByCode(54201);
            }
            return errorInfo;
        }
        //type 验证
        public ErrorInfo VerifyType(string type)
        {
            ErrorInfo errorInfo = new ErrorInfo { Code = 1 };

            if (!Enum.TryParse(typeof(EnumReplyType), type, true, out var t))
            {
                return APIHelper.GetErrorMessageByCode(54211);
            }
            return errorInfo;
        }
        //1. 只能有1个contactAgent 类型的item
        //2. item 数量不超过10个， 最少1个
        //3. item 验证
        public ErrorInfo VerifyCreate(Guid botId, QuickReplyCreateDto dto)
        {
            ErrorInfo errorInfo = VerifyType(dto.Type);
            if (errorInfo.Code != 1) return errorInfo;
            errorInfo = VerifyName(botId, dto.Name, Guid.Empty);
            if (errorInfo.Code != 1) return errorInfo;

            //item 数量不超过10个， 最少1个
            if (dto.Items != null && dto.Items.Length > 0 && dto.Items.Length <= 10)
            {
                foreach (var item in dto.Items)
                {
                    //3. item 验证
                    errorInfo = _quickReplyItemVerifyService.VerifyCreate(item);
                    if (errorInfo.Code != 1) return errorInfo;
                }
                var count = dto.Items.Count(t => t.Type.Equals("contactAgent", StringComparison.OrdinalIgnoreCase));
                if (count > 1)
                {
                    //1. 只能有1个contactAgent 类型的item
                    errorInfo = APIHelper.GetErrorMessageByCode(54203);
                }
            }
            else
            {
                errorInfo = APIHelper.GetErrorMessageByCode(54202);
            }
            return errorInfo;
        }      

        public ErrorInfo VerifyUpdate(QuickReplyUpdateDto dto)
        {
            ErrorInfo errorInfo = VerifyIdAndName(dto.Id, dto.Name);
            if (errorInfo.Code != 1) return errorInfo;
            errorInfo = VerifyType(dto.Type);
            if (errorInfo.Code != 1) return errorInfo;
            //item 数量不超过10个， 最少1个
            if (dto.Items != null && dto.Items.Count > 0 && dto.Items.Count <= 10)
            {
                foreach (var item in dto.Items)
                {
                    //3. item 验证
                    errorInfo = _quickReplyItemVerifyService.VerifyUpdate(item);
                    if (errorInfo.Code != 1) return errorInfo;
                }
                var count = dto.Items.Count(t => t.Type.Equals("contactAgent", StringComparison.OrdinalIgnoreCase));
                if (count > 1)
                {
                    //1. 只能有1个contactAgent 类型的item
                    return APIHelper.GetErrorMessageByCode(54203);
                }
            }
            else
            {
                return APIHelper.GetErrorMessageByCode(54202);
            }
            return errorInfo;
        }
        public ErrorInfo VerifyDelete(Guid id)
        {
            ErrorInfo errorInfo = VerifyId(id);
            //判断 quickreply 是否被引用
            return errorInfo;
        }
    }
}
